package com.qfedu.mall.service.Impl;

import cn.hutool.core.date.DateUtil;
import com.qfedu.mall.beans.entity.Order;
import com.qfedu.mall.beans.entity.UserAddr;
import com.qfedu.mall.common.utils.WxpayUtils;
import com.qfedu.mall.common.vo.OrderVo;
import com.qfedu.mall.common.vo.ResStatus;
import com.qfedu.mall.common.vo.ResultVO;
import com.qfedu.mall.mapper.OrderMapper;
import com.qfedu.mall.mapper.UserAddrMapper;
import com.qfedu.mall.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

/**
 * @author dyw
 * @Date 2021年12月02日 11:27
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserAddrMapper userAddrMapper;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVO orderInquiry(Integer userId, Integer status) {

        List<OrderVo> list = orderMapper.selectOrderByID(userId,status);
        ResultVO resultVO = new ResultVO(ResStatus.OK, "Success", list);
        return resultVO;
    }

    @Override
    public ResultVO buyNow(Integer productId, Integer userId, String goodsSize, Integer addrId,
                       Integer productNum,Double totalAmount,Double actualAmount,String remark) {
        String now = DateUtil.now();
        String orderId = now.replaceAll("-", "").replaceAll(" ","").replaceAll(":","");

        System.out.println(orderId);
        UserAddr userAddr = orderMapper.selectAddrByAddrId(addrId);
        String receiver_address = userAddr.getProvince() + userAddr.getCity() + userAddr.getArea() + userAddr.getAddr();
        Order order = new Order();
        order.setOrder_id(orderId);
        order.setUser_id(userId);
        order.setProduct_id(productId);
        order.setReceiver_name(userAddr.getReceiverName());
        order.setReceiver_mobile(userAddr.getReceiverMobile());
        order.setReceiver_address(receiver_address);
        order.setProduct_num(productNum);
        order.setGoods_size(goodsSize);
        order.setTotal_amount(totalAmount);
        order.setActual_amount(actualAmount);
        order.setPay_type(1);
        order.setOrder_remark(remark);
        order.setDelete_status(1);
        order.setDelivery_type("包邮");
//        order.setDelivery_flow_id();
        order.setCreate_time(now);
        System.out.println("---------->>" + order);

        int i = orderMapper.InterOrder(order);
        if (i > 0){
            String payUrl = "";
            try {
                payUrl = WxpayUtils.getPayUrl(orderId);
                System.out.println("wx url: "+ payUrl);

            }catch (Exception e){
                e.printStackTrace();
            }

            HashMap<String, Object> map = new HashMap<>();
            map.put("orderId",orderId);
            map.put("payUrl",payUrl);

            return new ResultVO(ResStatus.OK,"success",map);
        }else {
            return new ResultVO(ResStatus.NO,"fail","购买失败");
        }
    }
}
